package com.demo.mantis.service;

import cn.techarts.dragonfly.app.AbstractService;
import cn.techarts.dragonfly.data.trans.Transactional;
import com.demo.mantis.data.StudentDao;
import com.demo.mantis.model.Student;

import javax.inject.Singleton;
import java.util.Random;

@Singleton
public class StudentServiceImpl extends AbstractService implements StudentService {

//    @Override
//    public Student register(String name, String gender) {
//        if (name == null) return null;
//        var id = new Random().nextInt();
//        var result = new Student();
//        result.setId(id);
//        result.setName(name);
//        result.setGender(gender);
//        return result;
//    }
    @Override
    @Transactional
    public Student register(String name, String gender) {
        if (name == null) return null;
        //var helper = this.getDataHelper();

        var dao = this.getMapper(StudentDao.class);

        var result = new Student();
        result.setName(name);
        result.setGender(gender);
        //helper.save(result,"registerStudent");
        dao.registerStudent(result);

        return result;
    }

    @Override
    public Student findStudent(int id) {
        var dao = this.getMapper(StudentDao.class);
        return dao.getStudent(id);
    }

}
